package cn.Mmovie.servlet;

import cn.Mmovie.bean.Auditorium;
import cn.Mmovie.bean.Cinema;
import cn.Mmovie.service.AuditoriumService;
import cn.Mmovie.service.CinemaService;
import cn.Mmovie.service.impl.AuditoriumServiceImpl;
import cn.Mmovie.service.impl.CinemaServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AdminModifyAuditoriumServlet", value = "/admin/modify_auditorium")
public class AdminModifyAuditoriumServlet extends HttpServlet {
    private AuditoriumService auditoriumService=new AuditoriumServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("-------ModifyAuditorium----------");
        request.setCharacterEncoding("utf-8");
        Integer id=Integer.parseInt(request.getParameter("id"));
        String name=request.getParameter("name");
        Cinema cinema=new Cinema(Integer.parseInt(request.getParameter("cinema")));
        Integer capacity=Integer.parseInt(request.getParameter("capacity"));

        Auditorium auditorium=new Auditorium(id,cinema,name,capacity);
        Integer result=auditoriumService.modifyAuditorium(auditorium);
        if (result>0){
            request.setAttribute("msg","放映厅信息修改成功！");
            request.getRequestDispatcher("/admin/auditorium_list").forward(request,response);
        }else if (result==-1){
            request.setAttribute("failMsg","放映厅已存在！");
            request.getRequestDispatcher("/admin/auditorium_list").forward(request,response);
        }else {
            request.setAttribute("failMsg","放映厅信息修改失败，请重试！");
            request.getRequestDispatcher("/admin/auditorium_list").forward(request,response);
        }
    }
}
